package com.huitone.smspfm.interfacz.validator;



import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.interfacz.core.AppStaticConfigs;
import com.huitone.smspfm.interfacz.dao.entity.SvrNodeDef;
import com.huitone.smspfm.socket.core.verificate.Result;
import com.huitone.smspfm.socket.core.verificate.Validator;
import com.huitone.smspfm.socket.model.AccountInfo;
import com.huitone.smspfm.socket.protocol.sgip.constant.ErrorCode;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Bind;

public class UserValidator implements Validator {
	
	private static final Logger logger = LogManager.getLogger(UserValidator.class);
	
	public Result verificate(Bind bind,String ip){
		Result result = new Result();
		if(bind != null){
			String srvNode = bind.getSrvNode();
			if(checkIpValidity(srvNode, ip)){
				result = verificate(bind);
			} else {
				result.setErrorCode(ErrorCode.CODE_18);
				result.setAccept(false);
			}
		}
		
		return result;
	}

	public Result verificate(Bind bind) {
		
		Result result = new Result();
		
		if (bind != null) {
			
			// 这是一条Bind请求
			String srvNode = bind.getSrvNode();
			String username = bind.getLoginName();
			String password = bind.getLoginPassword();
			
			AccountInfo accountInfo = check(srvNode, username, password);
			
			if (accountInfo != null) {
				result.setAccountInfo(accountInfo);
				result.setErrorCode(ErrorCode.CODE0);
				result.setAccept(true);
			} else {
				result.setErrorCode(ErrorCode.CODE_17);
				result.setAccept(false);
			}
		}
		
		return result;
	}
	
	/**
	 * 验证白名单之外的ＩＰ合法性
	 * @param srvNode
	 * @param ip
	 * @return
	 */
	private boolean checkIpValidity(String srvNode, String ip){
		boolean result = false;
		
		Map<String, SvrNodeDef> map = AppStaticConfigs.svrNodeDefMap;
		if(map.containsKey(srvNode)){
			if(StringUtils.isEmpty(ip)){
				result = false;
			} else {
				SvrNodeDef svrNodeDef = map.get(srvNode);
				if(StringUtils.equals("0",svrNodeDef.getSrvtype())){//非白名单用户
					result = StringUtils.equals(ip,svrNodeDef.getServerIp());
				} else {
					result = true;
				}
			}
		} else {
			result = true;
		}
		return result;
	}
	
	public AccountInfo check(String srvNode, String username, String password) {
		
		AccountInfo accountInfo = null;
		
		Map<String, SvrNodeDef> map = AppStaticConfigs.svrNodeDefMap;
		
		if (map.containsKey(srvNode)) {
			
			SvrNodeDef svrNodeDef = map.get(srvNode);
			
			if (svrNodeDef.getLoginUser().equals(username) 
					&& svrNodeDef.getLoginPwd().equals(password)) {
				accountInfo = new AccountInfo();
				accountInfo.setSrvNode(srvNode);
				accountInfo.setLoginName(username);
				accountInfo.setLoginPassword(password);
				Integer maxFlux = svrNodeDef.getMaxFlux();
				accountInfo.setMaxFlux(maxFlux == null ? 0 : maxFlux);
				Integer conNum = svrNodeDef.getConNum();
				accountInfo.setMaxConn(conNum == null ? 0 : conNum);
				accountInfo.setLogin(true);
				
			} else {
				logger.warn("用户[" + username + "],业务端[" + srvNode + "]登录校验,密码[" + password + "]校验失败");
			}
		} else {
			logger.warn("用户[" + username + "],业务端[" + srvNode + "]登录校验,不存在的业务端编码");
		}
		
		return accountInfo;
	}

}
